using Newtonsoft.Json;
using System.Collections.Generic;
using BanZuiCloudControlAgent.Utils.Common;

namespace BanZuiCloudControlAgent.Core.Mqtt.Models
{
    /// <summary>
    /// 任务下发消息负载模型
    /// </summary>
    public class TaskDispatchPayloadModel
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        [JsonProperty("taskId")]
        public string TaskId { get; set; }

        /// <summary>
        /// 任务类型
        /// </summary>
        [JsonProperty("taskType")]
        public string TaskType { get; set; }

        /// <summary>
        /// 操作类型: 启动任务:START,暂停任务:PAUSE,停止任务:STOP
        /// </summary>
        [JsonProperty("operation")]
        public string Operation { get; set; }

        /// <summary>
        /// 任务优先级
        /// </summary>
        [JsonProperty("priority")]
        public int Priority { get; set; }

        /// <summary>
        /// 任务参数
        /// </summary>
        [JsonProperty("params")]
        public Dictionary<string, object> Params { get; set; }
    }

    /// <summary>
    /// 任务下发消息模型
    /// </summary>
    public class TaskDispatchModel : BaseMessageModel
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public TaskDispatchModel() : base()
        {
            MessageType = MessageTypeConstants.TASK_DISPATCH;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="payload">任务下发负载</param>
        public TaskDispatchModel(string deviceCode, TaskDispatchPayloadModel payload)
            : base(MessageTypeConstants.TASK_DISPATCH, deviceCode, payload)
        {
        }

        /// <summary>
        /// 获取任务下发负载
        /// </summary>
        /// <returns>任务下发负载</returns>
        public TaskDispatchPayloadModel GetPayload()
        {
            if (Payload == null)
                return null;
                
            if (Payload is TaskDispatchPayloadModel payloadModel)
                return payloadModel;
                
            // 尝试将Payload转换为TaskDispatchPayloadModel
            try
            {
                // 如果Payload是JObject或其他JSON对象，尝试转换
                return JsonHelper.Convert<object, TaskDispatchPayloadModel>(Payload);
            }
            catch
            {
                return null;
            }
        }
    }

    /// <summary>
    /// 加群任务参数模型
    /// </summary>
    public class AddGroupTaskParams
    {
        /// <summary>
        /// QQ号码
        /// </summary>
        [JsonProperty("qqNumber")]
        public string QQNumber { get; set; }

        /// <summary>
        /// 群号列表
        /// </summary>
        [JsonProperty("groupNumbers")]
        public List<string> GroupNumbers { get; set; }

        /// <summary>
        /// 入群消息
        /// </summary>
        [JsonProperty("joinMessage")]
        public string JoinMessage { get; set; }
    }

    /// <summary>
    /// 发送消息任务参数模型
    /// </summary>
    public class SendMessageTaskParams
    {
        /// <summary>
        /// QQ号码
        /// </summary>
        [JsonProperty("qqNumber")]
        public string QQNumber { get; set; }

        /// <summary>
        /// 目标类型（群/好友）
        /// </summary>
        [JsonProperty("targetType")]
        public string TargetType { get; set; }

        /// <summary>
        /// 目标ID列表（群号/好友QQ号）
        /// </summary>
        [JsonProperty("targetIds")]
        public List<string> TargetIds { get; set; }

        /// <summary>
        /// 消息内容
        /// </summary>
        [JsonProperty("message")]
        public string Message { get; set; }

        /// <summary>
        /// 是否包含图片
        /// </summary>
        [JsonProperty("hasImage")]
        public bool HasImage { get; set; }

        /// <summary>
        /// 图片URL列表
        /// </summary>
        [JsonProperty("imageUrls")]
        public List<string> ImageUrls { get; set; }
    }

    /// <summary>
    /// 邀请好友任务参数模型
    /// </summary>
    public class InviteFriendTaskParams
    {
        /// <summary>
        /// QQ号码
        /// </summary>
        [JsonProperty("friendQq")]
        public string FriendQQ { get; set; }

        /// <summary>
        /// 邀请间隔最大值
        /// </summary>
        [JsonProperty("inviteIntervalMax")]
        public int InviteIntervalMax { get; set; }

        /// <summary>
        /// 邀请间隔最小值
        /// </summary>
        [JsonProperty("inviteIntervalMin")]
        public int InviteIntervalMin { get; set; }
        /// <summary>
        /// 邀请数量
        /// </summary>
        [JsonProperty("inviteCount")]
        public int InviteCount { get; set; }

    }

    /// <summary>
    /// 转发消息任务参数模型
    /// </summary>
    public class ForwardMessageTaskParams
    {
        /// <summary>
        /// QQ号码
        /// </summary>
        [JsonProperty("qqNumber")]
        public string QQNumber { get; set; }

        /// <summary>
        /// 源群号
        /// </summary>
        [JsonProperty("sourceGroupNumber")]
        public string SourceGroupNumber { get; set; }

        /// <summary>
        /// 目标群号列表
        /// </summary>
        [JsonProperty("targetGroupNumbers")]
        public List<string> TargetGroupNumbers { get; set; }

        /// <summary>
        /// 消息关键词（用于匹配要转发的消息）
        /// </summary>
        [JsonProperty("messageKeywords")]
        public List<string> MessageKeywords { get; set; }

        /// <summary>
        /// 转发时间范围（分钟）
        /// </summary>
        [JsonProperty("forwardTimeRange")]
        public int ForwardTimeRange { get; set; }

        /// <summary>
        /// 是否自动转发新消息
        /// </summary>
        [JsonProperty("autoForwardNew")]
        public bool AutoForwardNew { get; set; }
    }
} 